/**
 * 
 */
package com.wolfpire.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.system.common.AuthorityUtils;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.Role;
import com.wolfpire.system.model.User;
import com.wolfpire.system.model.UserGroup;
import com.wolfpire.system.service.RoleService;
import com.wolfpire.system.service.UserGroupService;

/**
 * @author lihd
 *
 */

@Controller
@RequestMapping("/usergroup")
public class UserGroupController {
	
	@Resource private UserGroupService userGroupService;
	
	@RequestMapping("/list")
	public ModelAndView list(@PrincipalArgument User user, @RequestParam Long menuId) {
		//获取对应菜单的操作权限
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		return new ModelAndView("system/usergroup/usergroup_list", "operAuthorities", operAuthorities);
	}
	
	@ResponseBody
	@RequestMapping("/queryList")
	public EasyuiPagination<UserGroup> queryList(EasyuiPage<UserGroup> page, UserGroup filterUserGroup) {
		Page<UserGroup> userGroups = this.userGroupService.setPageDataList(page, filterUserGroup);
		return new EasyuiPagination<UserGroup>(userGroups);
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpSession session) {
		ModelAndView mv = new ModelAndView("system/usergroup/usergroup_add");
		return mv;
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(Long id) {
		ModelAndView mv = new ModelAndView("system/usergroup/usergroup_edit");
		if (null != id) {
			UserGroup usergroup = this.userGroupService.get(id);
			mv.getModelMap().put("usergroup", usergroup);
		}
		return mv;
	}
	/**
	 * 课程组关联学科
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/permission")
	public ModelAndView permission(Long id) {
		ModelAndView mv = new ModelAndView("system/usergroup/subject_assign");
		if (null != id) {
			UserGroup userGroup = this.userGroupService.get(id);
			mv.getModelMap().put("usergroup", userGroup);
		}
		return mv;
	}
	
	@RequestMapping(value = "/assignUser")
	public ModelAndView assignUser(Long id) {
		ModelAndView mv = new ModelAndView("system/usergroup/user_assign");
		if (null != id) {
			UserGroup userGroup = this.userGroupService.get(id);
			mv.getModelMap().put("usergroup", userGroup);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(Long id) {
		try {
			this.userGroupService.delete(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}

	@ResponseBody
	@RequestMapping("/save")
	public Map<String, Object> save(UserGroup newUserGroup) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean result = false;
		String msg = null;
		try {
			this.userGroupService.saveOrUpdate(newUserGroup);
			result = true;
			msg = "保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			msg = "保存失败，请联系管理员";
		}
		map.put("result", result);
		map.put("msg", msg);
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/isUnique")
	public Boolean isUnique(UserGroup userGroup) {
		try {
			boolean isUnique = this.userGroupService.isUnique(userGroup);
			return isUnique;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
